package com.guang.controller;

import com.guang.constant.MessageConstant;
import com.guang.entity.PageResult;
import com.guang.entity.QueryPageBean;
import com.guang.entity.Result;
import com.guang.pojo.CheckItem;
import com.guang.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/checkitem")
public class CheckItemController {

    @Autowired
    private CheckItemService checkItemService;

    // 查询所有检查项
    @RequestMapping(value = "/findAll")
    public Result findCheckItemList(){
        List<CheckItem> list = checkItemService.findAll();
        if(list!=null && list.size()>0){
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }else{
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    // 分页查询所有检查项
    @RequestMapping(value = "/findAllCheckItemFroCondition")
    public Result findAllCheckItemFroCondition(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = checkItemService.findAllCheckItemFroCondition(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
        if(pageResult!=null){
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        }else{
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //删除
    @GetMapping("/delete")
    public Result delete(Integer id){
        try {
            checkItemService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    // 跳转到检查项编辑页面
    @RequestMapping("/findById")
    public Result findById(Integer id){
        CheckItem checkItem = checkItemService.findById(id);
        if(checkItem!=null){
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        }
        else{
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    //编辑保存
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
            checkItemService.edit(checkItem);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

}